package com.atguigu.kafka.controller;

import com.agtguigu.pojo.MonitorInfo;
import com.alibaba.fastjson.JSON;
import com.atguigu.kafka.config.KafkaProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;







@RestController
public class AdminController {


    @Autowired
    private KafkaProducer kafkaProducer;

    /**
     *  开启了事务注解后可以使用这个注解或者通过实现接口的方式
     * @param message
     * @throws Exception
     */
    @Transactional
    @GetMapping("/publish")
    public void publishAdminMessage(@RequestParam("message") String message) throws Exception {
        System.out.println("开发发送消息");
        kafkaProducer.sendAdminMessage(message);

    }


    @GetMapping("/sendTransactionMessage")
    public void sendTransactionMessage(@RequestParam("message") String message) throws Exception {
        System.out.println("开发发送事务消息");
        kafkaProducer.sendTransactionMessage(message);

    }


    /**
     * 发送监控数据，
     * @param monitorInfo
     * @throws Exception
     */
    @PostMapping("/sendMonitorData")
    @Transactional
    public void sendMonitorData(@RequestBody MonitorInfo monitorInfo) throws Exception {

        String message = JSON.toJSONString(monitorInfo);

        System.out.println(message);

        kafkaProducer.sendAdminMessage(message);


    }







}
